package com.gary.spring.process;

import com.gary.spring.bean.Config;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author peng_rui
 * @date 2022/6/25
 */
public class MyComponentScanPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        try {
            ComponentScan componentScan = AnnotationUtils.findAnnotation(Config.class, ComponentScan.class);

            //用于根据BeanDefinition生成bean名称
            if (componentScan != null) {
                String[] packages = componentScan.basePackages();
                for (String aPackage : packages) {
                    String path = "classpath*:" + aPackage.replace(".", "/") + "/**/*.class";
                    System.out.println(path);

                    BeanNameGenerator generator = new AnnotationBeanNameGenerator();
                    //读取资源的元信息
                    CachingMetadataReaderFactory factory = new CachingMetadataReaderFactory();
                    Resource[] resources = new PathMatchingResourcePatternResolver().getResources(path);
                    for (Resource resource : resources) {
    //                    System.out.println("****************");
                        MetadataReader metadataReader = factory.getMetadataReader(resource);
                        //获取类元信息
                        ClassMetadata classMetadata = metadataReader.getClassMetadata();
    //                    System.out.println(classMetadata.getClassName());
                        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
    //                    //是否直接有指定的annotation
    //                    System.out.println("直接Component : " + annotationMetadata.hasAnnotation(Component.class.getName()));
    //                    //是否间接有指定的annotation  指定类的全限定名
    //                    System.out.println("间接接Component : " + annotationMetadata.hasMetaAnnotation("org.springframework.stereotype.Component"));


                        //判断类是否直接或间接又Component注解, 若包含测注入Spring容器
                        boolean flag = (annotationMetadata.hasAnnotation(Component.class.getName()) ||
                                annotationMetadata.hasMetaAnnotation(Component.class.getName()));
                        System.out.println(flag);
                        if (flag) {
                            //生成BeanDefinition
                            AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(classMetadata.getClassName())
                                    .getBeanDefinition();
                            //获取BeanDefinitionRegister
                            //生成bean 的名称
                            if (configurableListableBeanFactory instanceof DefaultListableBeanFactory) {
                                DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) configurableListableBeanFactory;
                                String beanName = generator.generateBeanName(beanDefinition, beanFactory);
                                beanFactory.registerBeanDefinition(beanName, beanDefinition);
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
